<template>
  <div class="container">
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片 -->
    <el-card class="box-card">
      <!-- 控件 -->
      <el-row>
        <el-col>
          <el-button
            type="primary"
            @click="ShowAddCateDialog"
          >添加分类</el-button>
        </el-col>
      </el-row>

      <!-- 表格 B -->
      <tree-table
        :data="cateList"
        :columns="columns"
        border
        :selection-type="false"
        show-index
        index-text="#"
        :expand-type="false"
      >

        <!-- 是否有效 -->
        <template
          slot="isValid"
          slot-scope="scope"
        >
          <i
            class="el-icon-circle-check"
            style="color: #67c23a"
            v-if="scope.row.cat_deleted === false"
          ></i>
          <i
            class="el-icon-circle-close"
            style="color: red"
            v-else
          ></i>
        </template>

        <!-- 排序 -->
        <template
          slot="paixun"
          slot-scope="scope"
        >
          <el-button
            type="success"
            size="mini"
            plain
            v-if="scope.row.cat_level === 0"
          >一级</el-button>

          <el-button
            type="warning"
            size="mini"
            plain
            v-else-if="scope.row.cat_level === 1"
          >二级</el-button>

          <el-button
            type="danger"
            size="mini"
            plain
            v-else
          >三级</el-button>
        </template>

        <!-- 操作 -->
        <template slot="caozuo">
          <el-button
            type="primary"
            icon="el-icon-edit"
            size="mini"
          >编辑</el-button>
          <el-button
            type="danger"
            icon="el-icon-delete"
            size="mini"
          >删除</el-button>
        </template>

      </tree-table>

      <!-- 表格 E -->

      <!-- 分页 B -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="querInfo.pagenum"
        :page-sizes="[5, 10, 20]"
        :page-size="querInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
      <!-- 分页 E -->

    </el-card>

    <!-- 添加分类 B -->
    <el-dialog
      title="添加分类"
      :visible.sync="AddDialogVisible"
      width="50%"
      @close="HideAddDialog"
    >
      <!-- 内容 B -->
      <el-form
        :model="AddCateForm"
        :rules="AddCateFormRules"
        ref="AddCateFormRef"
        label-width="80px"
      >

        <el-form-item
          label="分类名称"
          prop="cat_name"
        >
          <el-input v-model="AddCateForm.cat_name"></el-input>
        </el-form-item>

        <el-form-item label="父级分类">
          <el-cascader
            v-model="parentCateKeys"
            :options="parentCateList"
            :props="parentCateProps"
            clearable
            filterable
            @change="parentCateChange"
          ></el-cascader>
        </el-form-item>

      </el-form>
      <!-- 内容 E -->
      <span
        slot="footer"
        class="dialog-footer"
      >
        <el-button @click="AddDialogVisible = false">取 消</el-button>
        <el-button
          type="primary"
          @click="addCate"
        >确 定</el-button>
      </span>
    </el-dialog>
    <!-- 添加分类 E -->

  </div>
</template>

<script>

export default {
  data() {
    return {
      token: '',
      cateList: [],
      querInfo: {
        type: [1, 2, 3],
        pagenum: 1,
        pagesize: 5
      },
      total: 0,
      columns: [
        {
          label: '分类名称',
          prop: 'cat_name'
        },
        {
          label: '是否有效',
          maxWidth: '50px',
          type: 'template',
          template: 'isValid'
        },
        {
          label: '排序',
          maxWidth: '50px',
          type: 'template',
          template: 'paixun'
        },
        {
          label: '操作',
          minWidth: '100px',
          type: 'template',
          template: 'caozuo'
        }
      ],
      AddDialogVisible: false,
      // 添加分类 form 数据
      AddCateForm: {
        cat_name: '',
        cat_pid: 0,
        cat_level: 0
      },
      // 添加分类 校验规则
      AddCateFormRules: {
        cat_name: [
          { required: true, message: '请输入分类名称', trigger: 'blur' }
        ]
      },
      // 父级分类列表
      parentCateList: [],
      // 父级分类显示的规则
      parentCateProps: {
        'expandTrigger': 'hover',
        'value': 'cat_id',
        'label': 'cat_name',
        'children': 'children',
        'checkStrictly': true
      },
      parentCateKeys: []
    }
  },
  created() {
    this.token = window.sessionStorage.getItem('token')
    this.getCateList()
  },
  methods: {
    // 获取商品分类数据列表
    async getCateList() {
      const { data: res } = await this.$axios.get('categories', {
        params: this.querInfo,
        headers: { 'Authorization': this.token }
      })

      if (res.meta.status !== 200) {
        return this.$message.error(res.meta.msg)
      }

      this.cateList = res.data.result
      this.total = res.data.total

      // console.log(res.data)
    },
    handleSizeChange(val) {
      this.querInfo.pagesize = val
      this.getCateList()
    },
    handleCurrentChange(val) {
      this.querInfo.pagenum = val
      this.getCateList()
    },
    // 显示 添加弹窗
    ShowAddCateDialog() {
      this.getParentCateList()
      this.AddDialogVisible = true
    },
    // 关闭 添加弹窗
    HideAddDialog() {
      console.log('关闭')
      this.parentCateKeys = []
      this.AddCateForm.cat_pid = 0
      this.AddCateForm.cat_level = 0
      this.$refs.AddCateFormRef.resetFields()
    },
    // 获取父级分类数据
    async getParentCateList() {
      const { data: res } = await this.$axios.get('categories', {
        params: { type: 2 },
        headers: { 'Authorization': this.token }
      })
      if (res.meta.status !== 200) {
        return this.$message.error('获取父级分类数据失败')
      }
      this.parentCateList = res.data
      // console.log(res.data)
    },
    // 监听父级分类变化
    parentCateChange() {
      // console.log(this.parentCateKeys)

      if (this.parentCateKeys.length > 0) {
        console.log(this.parentCateKeys)
        this.AddCateForm.cat_pid = this.parentCateKeys[this.parentCateKeys.length - 1]
        this.AddCateForm.cat_level = this.parentCateKeys.length
      } else {
        this.AddCateForm.cat_pid = 0
        this.AddCateForm.cat_level = 0
      }
    },
    // 添加分类
    addCate() {
      // console.log(this.AddCateForm)
      this.$refs.AddCateFormRef.validate(async (valid) => {
        if (!valid) return

        const { data: res } = await this.$axios.post('categories', this.AddCateForm, {
          headers: { 'Authorization': this.token }
        })

        // console.log(res)
        if (res.meta.status !== 201) {
          return this.$message.error('添加分类失败')
        }

        this.$message.success('添加分类成功')
        this.AddDialogVisible = false
        this.getCateList()
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.el-cascader {
  width: 100%;
}
</style>